package digitRecognitionProblem.reduceConnections;

import genetic_algorithm.Chromosome;
import genetic_algorithm.Mutation;

import java.util.List;

import utils.RandomGenerator;

/**
 * A chromosome is mutated by flipping random bits in its BitSet
 */
public class ReduceConMutation implements Mutation {

	@Override
	public void mutate(List<Chromosome> chromosomes, double rate) {

		// mutate each chromosome according to mutation rate
		for(Chromosome c :chromosomes) {
			
			// scan current chromosome's values
			int numValues = c.getAllValues().size();
			for(int i = 0 ; i < numValues ; ++i) {
				
				// flip bit according to random chance
				if(RandomGenerator.nextDouble() < rate) {					
					((ReduceConChromosome) c).flipValue(i);
				}
			}
		}

	}

}
